package msg;

import static java.lang.Math.*;
import static org.junit.Assert.*;

import org.json.JSONObject;
import org.junit.Test;

import common.Pair;

@SuppressWarnings({"javadoc", "static-method"})
public class LocationTests {
	
	@Test
	public void testLocationDoubleDouble() {
		Location loc = new Location(0.5, 1);
		assertEquals(loc.getLatitude(), 0.5, 0);
		assertEquals(loc.getLongitude(), 1, 0);
		assertNull(loc.getAddress());
	}
	
	@Test
	public void testLocationDoubleDoubleString() {
		Location loc = new Location(0.5, 1, "add");
		assertEquals(loc.getLatitude(), 0.5, 0);
		assertEquals(loc.getLongitude(), 1, 0);
		assertEquals("add", loc.getAddress());
	}
	
	@Test
	public void testLocationJSONObject() {
		Location loc = new Location(new JSONObject("{lat: 0.5, long:1}"));
		assertEquals(loc.getLatitude(), 0.5, 0);
		assertEquals(loc.getLongitude(), 1, 0);
		assertNull(loc.getAddress());
		loc = new Location(new JSONObject("{lat: 0.5, long:1, address:'add'}"));
		assertEquals(loc.getLatitude(), 0.5, 0);
		assertEquals(loc.getLongitude(), 1, 0);
		assertEquals("add", loc.getAddress());
	}
	
	@Test
	public void testGetDistanceTo() {
		// from statue of liberty to eiffel tower
		Location statueOfLibery = new Location(40.6892, -74.0444);
		Location eiffelTower = new Location(48.8583, 2.2945);
		assertEquals(statueOfLibery.getDistanceTo(eiffelTower), eiffelTower.getDistanceTo(statueOfLibery), 0);
		assertEquals(statueOfLibery.getDistanceTo(eiffelTower), 5837 * 1000, 1000);
		
		// from haifa of liberty to TA
		Location haifa = new Location(32.8, 34.983333, "Haifa");
		Location telAviv = new Location(32.0833, 34.8000, "Tel-Aviv");
		assertTrue(haifa.getDistanceTo(telAviv) < (100 * 1000));
		assertTrue(haifa.getDistanceTo(telAviv) > (80 * 1000));
	}
	
	@Test
	public void testGetDistanceToTrivial() {
		// from statue of liberty to eiffel tower
		Location loc = new Location(1.1, 0.1);
		assertEquals(0.0, loc.getDistanceTo(loc), 0.1);
	}
	
	@Test
	public void testBounds() throws Exception {
		Location loc = new Location(80.0020969341, -39.9981836781);
		Pair<Location, Location> result = loc.getBoundingCoordinates(1000 * 1000);
		assertEquals(result.first.getLatitude(), (1.2393 * 180) / PI, 0.1);
		assertEquals(result.first.getLongitude(), (-1.8184 * 180) / PI, 0.1);
		assertEquals(result.second.getLatitude(), (1.5532 * 180) / PI, 0.1);
		assertEquals(result.second.getLongitude(), (0.4221 * 180) / PI, 0.1);
	}
	
}
